const scrollDown = function (elementId, t) {
    let target = document.getElementById(elementId);
    let scrollContainer = target;
    do { // find scroll container
        scrollContainer = scrollContainer.parentNode;
        if (!scrollContainer) return;
        scrollContainer.scrollTop += 1;
    } while (scrollContainer.scrollTop === 0);
    let targetY = 0;
    do { // find the top of target relatively to the container
        if (target === scrollContainer) break;
        targetY += target.offsetTop;
        target = target.offsetParent;
    } while (target);
    const scroll = function (c, a, b, i) {
        i++;
        if (i > 30) return;
        c.scrollTop = a + (b - a) / 30 * i;
        setTimeout(() => {
            scroll(c, a, b, i);
        }, 15);
    }
    // start scrolling
    scroll(scrollContainer, scrollContainer.scrollTop, targetY - t, 0);
}

const canvas = function () {
    const canvas = document.querySelector('canvas')
    const ctx = canvas.getContext('2d')
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    ctx.lineWidth = 0.3;
    ctx.strokeStyle = (new Color(150)).style;

    const mousePosition = {
        x: 30 * canvas.width / 100,
        y: 30 * canvas.height / 100
    };

    const dots = {
        nb: 750,
        distance: 50,
        d_radius: 100,
        array: []
    };

    function colorValue (min) {
        return Math.floor(Math.random() * 255 + min);
    }

    function createColorStyle (r, g, b) {
        return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
    }

    function mixComponents (comp1, weight1, comp2, weight2) {
        return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
    }

    function averageColorStyles (dot1, dot2) {
        const color1 = dot1.color;
        const color2 = dot2.color;

        const r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius);
        const g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius);
        const b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
        return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
    }

    function Color (min) {
        min = min || 0;
        this.r = colorValue(min);
        this.g = colorValue(min);
        this.b = colorValue(min);
        this.style = createColorStyle(this.r, this.g, this.b);
    }

    function Dot () {
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;

        this.vx = -0.5 + Math.random();
        this.vy = -0.5 + Math.random();

        this.radius = Math.random() * 2;
        this.color = new Color();
    }

    Dot.prototype = {
        draw: function () {
            ctx.beginPath();
            ctx.fillStyle = this.color.style;
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
            ctx.fill();
        }
    };

    function createDots () {
        for (let i = 0; i < dots.nb; i++) {
            dots.array.push(new Dot());
        }
    }

    function moveDots () {
        for (let i = 0; i < dots.nb; i++) {
            const dot = dots.array[i];
            if (dot.y < 0 || dot.y > canvas.height) {
                dot.vy = -dot.vy;
            } else if (dot.x < 0 || dot.x > canvas.width) {
                dot.vx = -dot.vx;
            }
            dot.x += dot.vx;
            dot.y += dot.vy;
        }
    }

    function connectDots () {
        for (let i = 0; i < dots.nb; i++) {
            for (let j = 0; j < dots.nb; j++) {
                const iDot = dots.array[i];
                const jDot = dots.array[j];

                if ((iDot.x - jDot.x) < dots.distance && (iDot.y - jDot.y) < dots.distance && (iDot.x - jDot.x) > -dots.distance && (iDot.y - jDot.y) > -dots.distance) {
                    if ((iDot.x - mousePosition.x) < dots.d_radius && (iDot.y - mousePosition.y) < dots.d_radius && (iDot.x - mousePosition.x) > -dots.d_radius && (iDot.y - mousePosition.y) > -dots.d_radius) {
                        ctx.beginPath();
                        ctx.strokeStyle = averageColorStyles(iDot, jDot);
                        ctx.moveTo(iDot.x, iDot.y);
                        ctx.lineTo(jDot.x, jDot.y);
                        ctx.stroke();
                        ctx.closePath();
                    }
                }
            }
        }
    }

    function drawDots () {
        for (let i = 0; i < dots.nb; i++) {
            const dot = dots.array[i];
            dot.draw();
        }
    }

    function animateDots () {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        moveDots();
        connectDots();
        drawDots();
        requestAnimationFrame(animateDots);
    }

    document.getElementById('canvas').onmousemove = function (e) {
        mousePosition.x = e.pageX;
        mousePosition.y = e.pageY;
    };
    // document.getElementById('canvas').onmouseleave = function (e) {
    //     mousePosition.x = canvas.width / 2;
    //     mousePosition.y = canvas.height / 2;
    // };

    createDots();
    requestAnimationFrame(animateDots);
}

export { scrollDown, canvas }
